<template>
    <el-dialog
        title="设备升级状态记录"
        :visible.sync="showValue"
        append-to-body
        :close-on-click-modal="false"
        width="800px"
        top="30px"
        :close="clearData"
        :before-close="clearData"
    >
        <div>
            <el-form
                ref="form"
                size="small"
                inline
                :model="form"
                label-width="80px"
            >
                <el-form-item label="sn码:" prop="sn">
                    <el-input v-model="form.sn" class="items"></el-input>
                </el-form-item>
                <el-form-item label="经营类型:" prop="terminalType">
                    <el-select
                        class="items"
                        v-model="form.terminalType"
                        placeholder="请选择"
                        size="small"
                    >
                        <el-option label="全部" value></el-option>
                        <el-option
                            v-for="(item, index) in terminalTypeList"
                            :key="index"
                            :label="item.terminalTypeName"
                            :value="item.terminalType"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-button
                    type="primary"
                    size="small"
                    @click="handleCurrentChange(1)"
                    >查询</el-button
                >
                <el-button
                    type="primary"
                    size="small"
                    plain
                    @click="resetFields"
                    >重置</el-button
                >
            </el-form>
            <el-table
                v-loading="loading"
                :header-cell-style="headerCellStyle"
                :data="tableData"
                size="small"
                max-height="350px"
            >
                <el-table-column
                    prop="sn"
                    label="SN码"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="versionCode"
                    label="版本号"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    prop="upgradePackageFileTypeName"
                    label="升级包类型"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    prop="upgradeTime"
                    label="升级时间"
                    show-overflow-tooltip
                >
                </el-table-column>
                <el-table-column
                    prop="statusName"
                    label="升级状态"
                    show-overflow-tooltip
                >
                </el-table-column>
            </el-table>
            <div style="width: 100%; padding: 15px" class="right">
                <el-pagination
                    background
                    :current-page.sync="position"
                    @current-change="handleCurrentChange"
                    :page-size="pageSize"
                    layout="prev, pager, next, total"
                    :total="totalNum"
                ></el-pagination>
            </div>
        </div>
    </el-dialog>
</template>

<script>
export default {
    props: {
        value: Boolean,
        currRow: Object
    },
    data () {
        return {
            form: {
                sn: '',
                terminalType: ''
            },
            terminalTypeList: [],
            position: 1,
            pageSize: 10,
            totalNum: 0,
            loading: false,
            tableData: [],
            showValue: false
        }
    },
    watch: {
        value (val) {
            this.showValue = val
        },
        showValue (val) {
            this.$emit('update:value', val)
            if (val) {
                this.getTerminalType()
                this.getList()
            }
        }
    },
    methods: {
        resetFields () {
            this.$refs['form'].resetFields()
            this.position = 1
            this.getList()
        },
        handleCurrentChange (page) {
            // 换页
            this.position = page
            this.getList()
        },
        // 获取列表
        async getTerminalType () {
            let res = await this.$http.get(this.$store.state.IP + '/system/company/getOnlyTerminalType?isAll=1', {
            })
            let response = res.data
            if (response.ret === '0') {
                this.terminalTypeList = response.data
            }
        },
        // 获取列表
        async getList () {
            this.loading = true
            let res = await this.$http.post(this.$store.state.IP + '/upgradeManage/deviceUpgradeRecord', {
                page: this.position - 1,
                size: this.pageSize,
                upgradeTaskID: this.currRow.upgradeTaskID,
                companyCode: this.currRow.companyCode,
                ...this.form
            })
            let response = res.data
            if (response.ret === '0') {
                this.loading = false
                this.tableData = response.res.list
                this.totalNum = response.res.total
            } else {
                this.loading = false
            }
        },
        // 清空数据
        clearData () {
            this.$refs['form'].resetFields()
            this.position = 1
            this.showValue = false
        }
    }
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
.items
    width: 150px

.right
    text-align: right
</style>